<?php


namespace App\Http\Controllers\Internal;


use App\Models\UserLesson;

class LessonController extends Controller
{
    /*
     * @api post /internal/lessons 购买成功后记录购买的课程
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'courseId' => 'required|integer',
            'userId' => 'required|integer',
            'lessonId' => 'required|integer',
            'sequence' => 'required|integer',
            'wordCount' => 'required|integer',
            'knowledgeCount' => 'required|integer',
            'essenceQaCount' => 'required|integer',
            'gameTestCount' => 'required|integer',
            'gameWordCount' => 'required|integer',
            'enableTime' => 'required|string',
        ]);
        $userId = $this->request->input('userId');
        $courseId = $this->request->input('courseId');
        $lessonId = $this->request->input('lessonId');
        $sequence = $this->request->input('sequence');
        $wordCount = $this->request->input('wordCount');
        $knowledgeCount = $this->request->input('knowledgeCount');
        $essenceQaCount = $this->request->input('essenceQaCount');
        $gameTestCount = $this->request->input('gameTestCount');
        $gameWordCount = $this->request->input('gameWordCount');
        $enableTime = $this->request->input('enableTime');

        $lessonObj = new UserLesson();
        $lessonObj->setUserId($userId);
        $lessonObj->setLessonId($lessonId);
        $lessonObj->setCourseId($courseId);
        $lessonObj->setSequence($sequence);
        $lessonObj->setWordCount($wordCount);
        $lessonObj->setKnowledgeCount($knowledgeCount);
        $lessonObj->setEssenceQaCount($essenceQaCount);
        $lessonObj->setGameTestCount($gameTestCount);
        $lessonObj->setGameWordCount($gameWordCount);
        $lessonObj->setEnableTime($enableTime);
        $lessonObj->save();

        return $this->sendSuccessJson($lessonObj->getId());
    }

    /*
     * @api get /internal/lessons/latest 获取一个用户某个课程中的最新课时信息
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function latest()
    {
        $this->validate($this->request, [
            'userId' => 'required|integer',
            'courseId' => 'required|integer',
        ]);
        $userId = $this->request->input('userId');
        $courseId = $this->request->input('courseId');
        $res = UserLesson::ofUserCourseLessons($userId, $courseId)
            ->orderBy('sequence', 'desc')->first();

        $item = [];
        if ($res) {
            $item = [
                'id' => $res->getId(),
                'lessonId' => $res->getLessonId(),
                'sequence' => $res->getSequence(),
            ];
        }

        return $this->sendSuccessJson($item);
    }

    /*
     * @api get /internal/lessons/count-pushed 获取用户已经推送的课时数
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function countPushed()
    {
        $this->validate($this->request, [
            'userId' => 'required|integer',
            'courseId' => 'required|integer',
        ]);
        $userId = $this->request->input('userId');
        $courseId = $this->request->input('courseId');
        $count = UserLesson::ofUserCourseOfficialLessons($userId, $courseId)->count();
        return $this->sendSuccessJson($count);
    }

    /*
     * @api get /internal/lessons/learned/yesterday 获取昨天学习的课时记录
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function learnedYesterday()
    {
        $this->validate($this->request, [
            'start' => 'required|string',
            'end' => 'required|string',
        ]);
        $start = $this->request->input('start');
        $end = $this->request->input('end');

        $res = UserLesson::where('first_study_time', '>=', $start)->where('first_study_time', '<=', $end)->get();

        $items = [];
        foreach($res as $v) {
            $items[$v->getId()] = [
                'userId' => $v->getUserId(),
                'wordCount' => $v->getWordCount(),
            ];
        }
        return $this->sendSuccessJson($items);
    }
}
